/*
 * BackupBatch.java
 *
 * Created on April 3, 2007, 5:27 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E;

import edu.byu.isys413.group1E.data.BackupBO;
import edu.byu.isys413.group1E.data.BackupDAO;
import edu.byu.isys413.group1E.data.CustomerBO;
import edu.byu.isys413.group1E.data.CustomerDAO;
import edu.byu.isys413.group1E.data.MembershipBO;
import edu.byu.isys413.group1E.data.MembershipDAO;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

/**
 *
 * @author tkulbeth
 */
public class BackupBatch {
    /**
     * Contains an instance of BackupBatch
     */
    private static BackupBatch instance = null;
    
    /**
     * Creates a new instance of BackupBatch
     */
    public BackupBatch() {
    }
    
    /**
     * Retrieves an instance of the BackupBatch object
     *
     * @return instance of BackupBatch
     */
    public static synchronized BackupBatch getInstance() {
        if (instance == null) {
            instance = new BackupBatch();
        }
        return instance;
    }
    
    
    /** Runs the batch processes needed for regular reminders of backup service status.
     */
    public synchronized void processBatch() throws Exception{
        //read in all of the members\
        ArrayList<MembershipBO> mList = MembershipDAO.getInstance().readAll();
        
        //get today's date for the begin date
        Calendar cal = Calendar.getInstance();
        java.util.Date today = cal.getTime();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String todayDate = fmt.format(today);
        Date now = Date.valueOf(todayDate);
        
        System.out.println("Beginning date checks: ");
        System.out.println("========================================================");
        for(int t=0;t<mList.size(); t++){
            if(mList.get(t).getBuExpDate() != null){    //if the member has backup service subscription
                Date exp = Date.valueOf(mList.get(t).getBuExpDate());   //get the expiration date and compare
                if(exp.before(now)){    //if the membership has expired
                    System.out.println("The Backup account expired as of: " + mList.get(t).getBuExpDate());
                    BackupBO bu = BackupHandler.getInstance().renewBackup(mList.get(t).getMembershipID());
                    System.out.println("The member's credit card has been charged for: " + bu.getPrice());
                    System.out.println("The Backup account has been renewed until: " + bu.getEndDate());
                    BackupDAO.getInstance().save(bu);   //save the new backup revenue source
                }
            }//end if for backup expiration date
            
            //now check the credit card on file's expiration date.
            String ccExpDate = new String(mList.get(t).getCreditCardExpYear() + "-" + 
                    mList.get(t).getCreditCardExpMonth() + "-01");
            Date ccExp = Date.valueOf(ccExpDate);
            if(ccExp.before(now)){  //if this credit card's expiration date has passed, need to send a notification
                CustomerBO cust = CustomerDAO.getInstance().readByMembershipID(mList.get(t).getMembershipID());
                System.out.println("\n----------------------------------");
                System.out.println("Customer " + cust.getCustFullName() + " needs to renew their credit card.");
                System.out.println("The following e-mail was generated and sent:\n\n***********\n" +
                        this.generateEmail(cust, ccExpDate) + "\n\n***********");
            }
        }
    }
    
    /** This method generates a string in a suitable format for an e-mail
     */
    private String generateEmail(CustomerBO cust, String date){
        String email = new String("Email: " + cust.getCustEmail() + "\n\nDear " + cust.getCustFullName() + ", \n\nIt has come to our attention that the " +
                "credit card you have on file with \"MyStuff Digital Life\" has expired " +
                "as of " + date + ".\nPlease renew it at your earliest convenience so we may continue " +
                "providing timely service in your behalf.\n\nSincerely, \nMyStuff Digital Life Management");
        return email;
    }
}
